package asia.lxq.cic.controller;


import asia.lxq.cic.config.GlobalConfig;
import asia.lxq.cic.dao.ArticleMapper;
import asia.lxq.cic.entity.Article;
import asia.lxq.cic.entity.Result;
import asia.lxq.cic.entity.StatusCode;
import asia.lxq.cic.entity.User;
import asia.lxq.cic.service.ArticleService;
import asia.lxq.cic.util.RedisUtil;
import asia.lxq.cic.util.UserDirUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    RedisUtil redisUtil;
    @Autowired
    ArticleMapper articleMapper;
    @Autowired
    ArticleService articleService;

    //todo 需要判断用户的资源文件夹是否存在
    @PostMapping("/upload_article_resource")
    public Result<Object> uploadArticleResource(@RequestPart("attach") Map<String, String> attach, @RequestPart("resourceFile") MultipartFile resourceFile) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user = (User) redisUtil.get(GlobalConfig.PRE_USERINFO_REDIS + authentication.getPrincipal());
        String resourceId = attach.get("resourceId");
        String type = attach.get("type");
        String resName = resourceId + "." + type;
        try {
            resourceFile.transferTo(new File(UserDirUtil.getUserResDir(user.getUserId(), resName)));
        } catch (IOException e) {
            return Result.buildOrigin(StatusCode.OTHER_ERR, null, "保存文件时发生错误：" + e.getMessage(), false);
        }
        return Result.buildOrigin(StatusCode.OK, null, UserDirUtil.getUserVirtualResDir(user.getUserId(), resName), true);
    }

    //todo 需要判断用户的资源文件夹是否存在
    @PostMapping("/upload_article")
    public Result<Object> uploadArticle(@RequestPart("article")Article article, @RequestPart("resourceFile") MultipartFile resourceFile) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user= (User) redisUtil.get(GlobalConfig.PRE_USERINFO_REDIS+authentication.getPrincipal());
        String resourceId = article.getArticleResourceId();
        String resName="article_cover_"+resourceId+".png";
        article.setArticleAuthorId(user.getUserId());
        article.setArticleThumbUp(0);
        article.setArticleCollegeId(Integer.parseInt(user.getBelong().split(",")[2]));
        articleMapper.insert(article);
        try {
            resourceFile.transferTo(new File(UserDirUtil.getUserResDir(user.getUserId(),resName)));
        } catch (IOException e) {
            return Result.buildOrigin(StatusCode.OTHER_ERR,null,"保存文件时发生错误："+e.getMessage(),false);
        }
        return Result.buildOrigin(StatusCode.OK,article.getArticleId(),"发布成功",true);
    }
    @PostMapping("/update_article")
    public Result<Object> updateArticle(@RequestPart("article")Article article,
                                        @RequestPart(value = "resourceFile",required = false) MultipartFile resourceFile) {
        return articleService.updateArticle(article,resourceFile);
    }

    @GetMapping("/user_article_list")
    public Result<List<Article>> getUserArticleList() {
        return articleService.getUserArticleList();
    }

    @GetMapping("/article_thumb")
    public Result<Object> thumbUp(int articleId) {
        return articleService.thumbUp(articleId);
    }

}
